{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nAuto-tuning a Convolutional Network for NVIDIA GPU\n==================================================\n**Author**: `Lianmin Zheng <https://github.com/merrymercy>`_, `Eddie Yan <https://github.com/eqy/>`_\n\nAuto-tuning for specific devices and workloads is critical for getting the\nbest performance. This is a tutorial on how to tune a whole convolutional\nnetwork for NVIDIA GPU.\n\nThe operator implementation for NVIDIA GPU in TVM is written in template form.\nThe template has many tunable knobs (tile factor, unrolling, etc).\nWe will tune all convolution and depthwise convolution operators\nin the neural network. After tuning, we produce a log file which stores\nthe best knob values for all required operators. When the TVM compiler compiles\nthese operators, it will query this log file to get the best knob values.\n\nWe also released pre-tuned parameters for some NVIDIA GPUs. You can go to\n`NVIDIA GPU Benchmark <https://github.com/apache/tvm/wiki/Benchmark#nvidia-gpu>`_\nto see the results.\n\nNote that this tutorial will not run on Windows or recent versions of macOS. To\nget it to run, you will need to wrap the body of this tutorial in a :code:`if\n__name__ == \"__main__\":` block.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Install dependencies\n--------------------\nTo use the autotvm package in tvm, we need to install some extra dependencies.\n(change \"3\" to \"2\" if you use python2):\n\n.. code-block:: bash\n\n  pip3 install --user psutil xgboost tornado cloudpickle\n\nTo make TVM run faster during tuning, it is recommended to use cython\nas FFI of tvm. In the root directory of tvm, execute:\n\n.. code-block:: bash\n\n  pip3 install --user cython\n  sudo make cython3\n\nNow return to python code. Import packages.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\nimport numpy as np\n\nimport tvm\nfrom tvm import relay, autotvm\nimport tvm.relay.testing\nfrom tvm.autotvm.tuner import XGBTuner, GATuner, RandomTuner, GridSearchTuner\nimport tvm.contrib.graph_executor as runtime"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define Network\n--------------\nFirst we need to define the network in relay frontend API.\nWe can load some pre-defined network from :code:`tvm.relay.testing`.\nWe can also load models from MXNet, ONNX and TensorFlow.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def get_network(name, batch_size):\n    \"\"\"Get the symbol definition and random weight of a network\"\"\"\n    input_shape = (batch_size, 3, 224, 224)\n    output_shape = (batch_size, 1000)\n\n    if \"resnet\" in name:\n        n_layer = int(name.split(\"-\")[1])\n        mod, params = relay.testing.resnet.get_workload(\n            num_layers=n_layer, batch_size=batch_size, dtype=dtype\n        )\n    elif \"vgg\" in name:\n        n_layer = int(name.split(\"-\")[1])\n        mod, params = relay.testing.vgg.get_workload(\n            num_layers=n_layer, batch_size=batch_size, dtype=dtype\n        )\n    elif name == \"mobilenet\":\n        mod, params = relay.testing.mobilenet.get_workload(batch_size=batch_size, dtype=dtype)\n    elif name == \"squeezenet_v1.1\":\n        mod, params = relay.testing.squeezenet.get_workload(\n            batch_size=batch_size, version=\"1.1\", dtype=dtype\n        )\n    elif name == \"inception_v3\":\n        input_shape = (batch_size, 3, 299, 299)\n        mod, params = relay.testing.inception_v3.get_workload(batch_size=batch_size, dtype=dtype)\n    elif name == \"mxnet\":\n        # an example for mxnet model\n        from mxnet.gluon.model_zoo.vision import get_model\n\n        block = get_model(\"resnet18_v1\", pretrained=True)\n        mod, params = relay.frontend.from_mxnet(block, shape={\"data\": input_shape}, dtype=dtype)\n        net = mod[\"main\"]\n        net = relay.Function(\n            net.params, relay.nn.softmax(net.body), None, net.type_params, net.attrs\n        )\n        mod = tvm.IRModule.from_expr(net)\n    else:\n        raise ValueError(\"Unsupported network: \" + name)\n\n    return mod, params, input_shape, output_shape"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set Tuning Options\n------------------\nBefore tuning, we apply some configurations.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "#### DEVICE CONFIG ####\ntarget = tvm.target.cuda()\n\n#### TUNING OPTION ####\nnetwork = \"resnet-18\"\nlog_file = \"%s.log\" % network\ndtype = \"float32\"\n\ntuning_option = {\n    \"log_filename\": log_file,\n    \"tuner\": \"xgb\",\n    \"n_trial\": 2000,\n    \"early_stopping\": 600,\n    \"measure_option\": autotvm.measure_option(\n        builder=autotvm.LocalBuilder(timeout=10),\n        runner=autotvm.LocalRunner(number=20, repeat=3, timeout=4, min_repeat_ms=150),\n    ),\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>How to set tuning options\n\n  In general, the default value provided here works well.\n\n  If you have large time budget, you can set :code:`n_trial`, :code:`early_stopping` larger,\n  which makes the tuning runs longer.\n\n  If you have multiple devices, you can use all of them for measurement to\n  accelerate the tuning process. (see the 'Scale up measurement` section below).</p></div>\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Begin Tuning\n------------\nNow we can extract tuning tasks from the network and begin tuning.\nHere, we provide a simple utility function to tune a list of tasks.\nThis function is just an initial implementation which tunes them in sequential order.\nWe will introduce a more sophisticated tuning scheduler in the future.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# You can skip the implementation of this function for this tutorial.\ndef tune_tasks(\n    tasks,\n    measure_option,\n    tuner=\"xgb\",\n    n_trial=1000,\n    early_stopping=None,\n    log_filename=\"tuning.log\",\n    use_transfer_learning=True,\n):\n    # create tmp log file\n    tmp_log_file = log_filename + \".tmp\"\n    if os.path.exists(tmp_log_file):\n        os.remove(tmp_log_file)\n\n    for i, tsk in enumerate(reversed(tasks)):\n        prefix = \"[Task %2d/%2d] \" % (i + 1, len(tasks))\n\n        # create tuner\n        if tuner == \"xgb\" or tuner == \"xgb-rank\":\n            tuner_obj = XGBTuner(tsk, loss_type=\"rank\")\n        elif tuner == \"ga\":\n            tuner_obj = GATuner(tsk, pop_size=100)\n        elif tuner == \"random\":\n            tuner_obj = RandomTuner(tsk)\n        elif tuner == \"gridsearch\":\n            tuner_obj = GridSearchTuner(tsk)\n        else:\n            raise ValueError(\"Invalid tuner: \" + tuner)\n\n        if use_transfer_learning:\n            if os.path.isfile(tmp_log_file):\n                tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file))\n\n        # do tuning\n        tsk_trial = min(n_trial, len(tsk.config_space))\n        tuner_obj.tune(\n            n_trial=tsk_trial,\n            early_stopping=early_stopping,\n            measure_option=measure_option,\n            callbacks=[\n                autotvm.callback.progress_bar(tsk_trial, prefix=prefix),\n                autotvm.callback.log_to_file(tmp_log_file),\n            ],\n        )\n\n    # pick best records to a cache file\n    autotvm.record.pick_best(tmp_log_file, log_filename)\n    os.remove(tmp_log_file)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, we launch tuning jobs and evaluate the end-to-end performance.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def tune_and_evaluate(tuning_opt):\n    # extract workloads from relay program\n    print(\"Extract tasks...\")\n    mod, params, input_shape, out_shape = get_network(network, batch_size=1)\n    tasks = autotvm.task.extract_from_program(\n        mod[\"main\"], target=target, params=params, ops=(relay.op.get(\"nn.conv2d\"),)\n    )\n\n    # run tuning tasks\n    print(\"Tuning...\")\n    tune_tasks(tasks, **tuning_opt)\n\n    # compile kernels with history best records\n    with autotvm.apply_history_best(log_file):\n        print(\"Compile...\")\n        with tvm.transform.PassContext(opt_level=3):\n            lib = relay.build_module.build(mod, target=target, params=params)\n\n        # load parameters\n        dev = tvm.device(str(target), 0)\n        module = runtime.GraphModule(lib[\"default\"](dev))\n        data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))\n        module.set_input(\"data\", data_tvm)\n\n        # evaluate\n        print(\"Evaluate inference time cost...\")\n        print(module.benchmark(dev, number=1, repeat=600))\n\n\n# We do not run the tuning in our webpage server since it takes too long.\n# Uncomment the following line to run it by yourself.\n\n# tune_and_evaluate(tuning_option)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Sample Output\n-------------\nThe tuning needs to compile many programs and extract feature from them.\nSo a high performance CPU is recommended. One sample output is listed below.\nIt takes about 4 hours to get the following output on a 32T AMD Ryzen Threadripper.\nThe tuning target is NVIDIA 1080 Ti.\n(You can see some errors during compilation. If the tuning is not stuck, it is okay.)\n\n.. code-block:: bash\n\n   Extract tasks...\n   Tuning...\n   [Task  1/12]  Current/Best:  541.83/3570.66 GFLOPS | Progress: (960/2000) | 1001.31 s Done.\n   [Task  2/12]  Current/Best:    0.56/ 803.33 GFLOPS | Progress: (704/2000) | 608.08 s Done.\n   [Task  3/12]  Current/Best:  103.69/1141.25 GFLOPS | Progress: (768/2000) | 702.13 s Done.\n   [Task  4/12]  Current/Best: 2905.03/3925.15 GFLOPS | Progress: (864/2000) | 745.94 sterminate called without an active exception\n   [Task  4/12]  Current/Best: 2789.36/3925.15 GFLOPS | Progress: (1056/2000) | 929.40 s Done.\n   [Task  5/12]  Current/Best:   89.06/1076.24 GFLOPS | Progress: (704/2000) | 601.73 s Done.\n   [Task  6/12]  Current/Best:   40.39/2129.02 GFLOPS | Progress: (1088/2000) | 1125.76 s Done.\n   [Task  7/12]  Current/Best: 4090.53/5007.02 GFLOPS | Progress: (800/2000) | 903.90 s Done.\n   [Task  8/12]  Current/Best:    4.78/1272.28 GFLOPS | Progress: (768/2000) | 749.14 s Done.\n   [Task  9/12]  Current/Best: 1391.45/2325.08 GFLOPS | Progress: (992/2000) | 1084.87 s Done.\n   [Task 10/12]  Current/Best: 1995.44/2383.59 GFLOPS | Progress: (864/2000) | 862.60 s Done.\n   [Task 11/12]  Current/Best: 4093.94/4899.80 GFLOPS | Progress: (224/2000) | 240.92 sterminate called without an active exception\n   [Task 11/12]  Current/Best: 3487.98/4909.91 GFLOPS | Progress: (480/2000) | 534.96 sterminate called without an active exception\n   [Task 11/12]  Current/Best: 4636.84/4912.17 GFLOPS | Progress: (1184/2000) | 1381.16 sterminate called without an active exception\n   [Task 11/12]  Current/Best:   50.12/4912.17 GFLOPS | Progress: (1344/2000) | 1602.81 s Done.\n   [Task 12/12]  Current/Best: 3581.31/4286.30 GFLOPS | Progress: (736/2000) | 943.52 s Done.\n   Compile...\n   Evaluate inference time cost...\n   Mean inference time (std dev): 1.07 ms (0.05 ms)\n\nAs a reference baseline, the time cost of MXNet + TensorRT on resnet-18 is 1.30ms. So we are a little faster.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>**Experiencing Difficulties?**\n\n  The auto tuning module is error-prone. If you always see \" 0.00/ 0.00 GFLOPS\",\n  then there must be something wrong.\n\n  First, make sure you set the correct configuration of your device.\n  Then, you can print debug information by adding these lines in the beginning\n  of the script. It will print every measurement result, where you can find useful\n  error messages.\n\n  .. code-block:: python\n\n     import logging\n     logging.getLogger('autotvm').setLevel(logging.DEBUG)\n\n  Finally, always feel free to ask our community for help on https://discuss.tvm.apache.org</p></div>\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Scale up measurement by using multiple devices\n----------------------------------------------\nIf you have multiple devices, you can use all of them for measurement.\nTVM uses the RPC Tracker to manage distributed devices.\nThe RPC Tracker is a centralized controller node. We can register all devices to\nthe tracker. For example, if we have 10 GPU cards, we can register all of them\nto the tracker, and run 10 measurements in parallel, accelerating the tuning process.\n\nTo start an RPC tracker, run this command on the host machine. The tracker is\nrequired during the whole tuning process, so we need to open a new terminal for\nthis command:\n\n.. code-block:: bash\n\n  python -m tvm.exec.rpc_tracker --host=0.0.0.0 --port=9190\n\nThe expected output is\n\n.. code-block:: bash\n\n  INFO:RPCTracker:bind to 0.0.0.0:9190\n\nThen open another new terminal for the RPC server. We need to start one dedicated server\nfor each device. We use a string key to distinguish the types of devices.\nYou can pick a name you like.\n(Note: For rocm backend, there are some internal errors with the compiler,\nwe need to add `--no-fork` to the argument list.)\n\n.. code-block:: bash\n\n    python -m tvm.exec.rpc_server --tracker=127.0.0.1:9190 --key=1080ti\n\nAfter registering devices, we can confirm it by querying rpc_tracker\n\n.. code-block:: bash\n\n  python -m tvm.exec.query_rpc_tracker --host=127.0.0.1 --port=9190\n\nFor example, if we have four 1080ti, two titanx and one gfx900, the output can be\n\n.. code-block:: bash\n\n   Queue Status\n   ----------------------------------\n   key          total  free  pending\n   ----------------------------------\n   1080ti       4      4     0\n   titanx       2      2     0\n   gfx900       1      1     0\n   ----------------------------------\n\nFinally, we need to change the tuning option to use RPCRunner. Use the code below\nto replace the corresponding part above.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tuning_option = {\n    \"log_filename\": log_file,\n    \"tuner\": \"xgb\",\n    \"n_trial\": 2000,\n    \"early_stopping\": 600,\n    \"measure_option\": autotvm.measure_option(\n        builder=autotvm.LocalBuilder(timeout=10),\n        runner=autotvm.RPCRunner(\n            \"1080ti\",  # change the device key to your key\n            \"127.0.0.1\",\n            9190,\n            number=20,\n            repeat=3,\n            timeout=4,\n            min_repeat_ms=150,\n        ),\n    ),\n}"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}